package com.tentact.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Dbutil {
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/hsys?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 ";
    private static final String user ="root";
    private static final String pass = "root";

    //鍔犺浇椹卞姩
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //鑾峰彇杩炴帴
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL,user,pass);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //鍏抽棴璧勬簮
    public static void close(Connection connection) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Connection connection, PreparedStatement statement) {
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet set) {
        try {
            set.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //澧炲垹鏀归�氱敤鏂规硶
    public static int executeUpdate(String sql, Object[] paras) {
        int result = 0;
        Connection connection = getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    statement.setObject(i+1, paras[i]);
                }
            }
            result = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(connection, statement);
        }
        return result;
    }

    //鏌ヨ杩斿洖闆嗗悎
    public static<T> List<T> executeQuery(String sql, Object[] paras, RowMapper<T> mapper){
        Connection connection = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<T> list = new ArrayList<T>();

        try {
            statement = connection.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    statement.setObject(i+1, paras[i]);
                }
            }

            rs = statement.executeQuery();
            while (rs.next()) {
                T object = mapper.getMapper(rs);
                list.add(object);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(connection, statement, rs);
        }

        return list;
    }

    //鏍规嵁缂栧彿鏌ヨ
    public static<T> T executeQueryById(String sql, Object[] paras, RowMapper<T> mapper) {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        T objec = null;
        try {
            statement = connection.prepareStatement(sql);
            if (paras != null) {
                for (int i = 0; i < paras.length; i++) {
                    statement.setObject(i+1, paras[i]);
                }
            }

            rs = statement.executeQuery();
            if (rs.next()) {
                objec = mapper.getMapper(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(connection, statement,rs);
        }

        return objec;
    }








}
